package com.lb.jwitter.action;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblCTDTKhoiNganhMonHoc;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocKem;
import com.lb.jwitter.service.TblCTDTMonHocKemService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHKetquaService;
import com.lb.jwitter.service.TblDKMHLogKetQuaDangKy;
import com.lb.jwitter.service.TblDKMHLogKetQuaDangKyService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.service.TblThongTinTKB;
import com.lb.jwitter.service.TblThongTinTKBService;

import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.CTDT;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.QuyenDangKy;
import com.lb.jwitter.util.SendMail;
import com.lb.jwitter.util.ThoiKhoaBieuDuKien;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class XacNhanKetQuaDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	
	private List<KetQuaDangKy> ketQuaDangKys;
	public List<KetQuaDangKy> getKetQuaDangKys() {
		return ketQuaDangKys;
	}

	public void setKetQuaDangKys(List<KetQuaDangKy> ketQuaDangKys) {
		this.ketQuaDangKys = ketQuaDangKys;
	}

	private int soTietTrongNgay;

	public int getSoTietTrongNgay() {
		return soTietTrongNgay;
	}

	public void setSoTietTrongNgay(int soTietTrongNgay) {
		this.soTietTrongNgay = soTietTrongNgay;
	}

	// ERROR, SUCCESS, CONFIRM
	private String code;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	private String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{
		try {
			

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	     Integer dotDKId = (Integer) session.get("dotDKId");
	     
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    TblDKMHKetquaService tblDKMHKetquaService = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient");
	    TblDKMHLogKetQuaDangKyService tblDKMHLogKetQuaDangKyService = (TblDKMHLogKetQuaDangKyService) context.getBean("tbl_DKMH_LogKetQuaDangKyClient");
	    TblThongTinTKBService tblThongTinTKBService = (TblThongTinTKBService) context.getBean("tbl_ThongTin_TKBClient");
	    TblCTDTMonHocKemService tblCTDTMonHocKemService = (TblCTDTMonHocKemService) context.getBean("tbl_CTDT_MonHocKemClient");

	    TblThongTinMonHocService tblThongTinMonHocService = (TblThongTinMonHocService)  context.getBean("tbl_ThongTin_MonHocClient");

		TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService) 
										context.getBean("tbl_ThongTin_HocVien_MonHocClient");
		
		Boolean trongHanDK = (Boolean) session.get("trongHanDK");
		boolean capnhat = false;
		//NEU TRONG HAN DK MOI CHO PHEP XAC NHAN
	    if (trongHanDK) {
		    ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
		    
		    /****************************************************************************************
		     * KIEM TRA MON HOC COT LOI
		     * Neu dot DK co mo mon cot loi && chua dat && chua dang ky 1 mon cot loi nao khac thuoc cung nhom cot loi
		     *   --> khong duoc phep xac nhan
		     ****************************************************************************************/
		    CTDT ctdt = (CTDT) session.get("CTDT");
		    
		    //danh sach mon cot loi theo CTDT
		    List<TblCTDTKhoiNganhMonHoc> monCotLois = new ArrayList<TblCTDTKhoiNganhMonHoc>();

		    for(TblCTDTKhoiNganhMonHoc khoiNganhMH : ctdt.getMonHocs()){
		    	if(khoiNganhMH.getMaCotLoi() != null && !khoiNganhMH.getMaCotLoi().equals("")){
		    		monCotLois.add(khoiNganhMH);
		    	}
		    }
		    
		    for(TblCTDTKhoiNganhMonHoc monCotLoi1 : monCotLois){
		    	
		    		//danh sach mon hoc cung nhom cot loi
		    		List<TblCTDTKhoiNganhMonHoc> nhomCotLoi = new ArrayList<TblCTDTKhoiNganhMonHoc>();
		    		for(TblCTDTKhoiNganhMonHoc monCotLoi2 : monCotLois){
		    			if(monCotLoi2.getMaCotLoi().equals(monCotLoi1.getMaCotLoi())){
    						nhomCotLoi.add(monCotLoi2);
    					}
		    		}
		    		
		    		//neu nhom cot loi da duoc dang ky thi thoi
		    		boolean daDKNhomCotLoi = false;
		    		for(KetQuaDangKy ketqua : ketQuaDangKys){
		    			//kiem tra mon da DK co mon thuoc cung nhom cot loi khong
		    			for(TblCTDTKhoiNganhMonHoc monCotLoi2 : nhomCotLoi){
		    				//neu mon da dang ky thuoc nhom cot loi
		    				if((ketqua.getKetqua().getMaDieuChinh().equals("BS") || ketqua.getKetqua().getMaDieuChinh().equals("HC")) 
		    						&& ketqua.getKetqua().getNLMHId().getMonHocId().getId().equals(monCotLoi2.getMonHocId().getId())){
		    					
		    					daDKNhomCotLoi = true;
		    					break;
		    				}
		    			}
		    			
		    		}
		    		
		    		if(!daDKNhomCotLoi){
			    		String kq = tblThongTinHocVienMonHocService
			    				.calSPKiemTraKetQuaMonHoc(	tblDKMHTaiKhoanHocVien.getHocVienId().getMSSV(), 
			    											monCotLoi1.getMonHocId().getMa());
			    		//ket qua mon cot loi chua hoc hoac chua dat
			    		if(kq == null || kq.equals("FAIL")){
			    			//neu co mo lop mon hoc thuoc nhom cot loi thi bao loi
			    			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			    		    { 
			    		        ConditionReport con = new ConditionReport(); 
			    		        con.setCol("dotDKId.id");
			    		        con.setValue1(new Long(dotDKId));
			    		        con.setOperator("eq");
			    		        cons.add(con);	
			    		    }
			    		    
			    		    String danhSachIdMonHoc = "";
			    		    for(TblCTDTKhoiNganhMonHoc monCotLoi3 : nhomCotLoi){
			    			    danhSachIdMonHoc += monCotLoi3.getMonHocId().getId() +";";
			    		    }
			    		    
			    		    { 
			    		        ConditionReport con = new ConditionReport(); 
			    		        con.setCol("monHocId.id");
			    		        con.setType("Long");
			    		        con.setValue1(danhSachIdMonHoc);
			    		        con.setOperator("in");
			    		        cons.add(con);	
			    		    }
			    		    
			    		    int c = tblThongTinMonHocService.counttblThongTinMonHocByCondition(cons);
			    		    if(c > 0){
			    		    	code = "ERROR";
			    				//chua dang ky mon cot loi
			    				msg = "<b>Không thể xác nhận </b> <br/> Bạn bắt buộc phải đăng ký 1 môn cốt lõi thuộc nhóm " +monCotLoi1.getMaCotLoi()+ "!";
			    				return Action.ERROR;
			    		    }
			    		}
		    		}
		    
		    }
		    
		    //danh sach mon cot loi co mo && chua dang ky && chua dat
		    
		    
		    List<TblDKMHLogKetQuaDangKy> tblDKMHLogKetQuaDangKies = new ArrayList<TblDKMHLogKetQuaDangKy>();
		    
		    for(KetQuaDangKy ketqua : ketQuaDangKys){
		    		
		    		if(ketqua.getKetqua().getId() != null && !ketqua.getKetqua().isXacNhanYN()){
		    			//bien Flag de kiem tra co cap nhat -> gui mail
		    			capnhat = true;
		    			
		    			
		    			if(ketqua.getKetqua().getMaDieuChinh().equals("BH") || ketqua.getKetqua().getMaDieuChinh().equals("HH")){
			    			if(ketqua.getKetqua().getTinhTrang().equals("RN"))
			    				tblDKMHKetquaService.deletetblDKMHKetqua(ketqua.getKetqua().getId());
			    			else{
				    			ketqua.getKetqua().setXacNhanYN(true);
				    			tblDKMHKetquaService.updatetblDKMHKetqua(ketqua.getKetqua());
			    			}
	
		    			}else{
		    				if(!ketqua.getKetqua().getKetQua().equals("0")){
		    					ketqua.getKetqua().setKetQua("*");
		    					ketqua.getKetqua().setKetQuaLyDo("Đăng ký thành công!");
		    				}
	    					//backup ket qua
	    					ketqua.getKetqua().setKetQua2(ketqua.getKetqua().getKetQua());
	    					ketqua.getKetqua().setKetQuaLyDo2(ketqua.getKetqua().getKetQuaLyDo());
		    				
		    				boolean error = false;
		    				List<ConditionReport> cons = new ArrayList<ConditionReport>();

		    				List<String> columns = new ArrayList<String>();
		    				
		    				/*********************************************
		    				 * kiem tra mon hoc kem theo
		    				 *********************************************/
		    				if(!error){
			    				{ 
			    			        ConditionReport con = new ConditionReport(); 
			    			        con.setCol("monHocId.id");
			    			        con.setValue1(ketqua.getKetqua().getNLMHId().getMonHocId().getId());
			    			        con.setOperator("eq");
			    			        cons.add(con);	
			    			    }
			    				List<TblCTDTMonHocKem> mhKems = tblCTDTMonHocKemService.findtblCTDTMonHocKemByCondition(cons, 1, 1);
			    				
			    				boolean daDkMHKem = false;
			    				String lyDo = "";
			    				for(TblCTDTMonHocKem mhKem : mhKems){
			    					for(KetQuaDangKy kqdk : ketQuaDangKys){
			    						if(kqdk.getKetqua().getNLMHId().getMonHocId().getId().equals(mhKem.getMonHocKemId().getId())){
			    							daDkMHKem = true;
			    							break;
			    						}
			    					}
			    					lyDo = "Chưa đăng ký môn học kèm " + mhKem.getMonHocKemId().getMa() + 
	    									" - " + mhKem.getMonHocKemId().getTen();
			    				}
			    				//neu co MH kem va chua DK
			    				if(mhKems != null && mhKems.size() > 0 && !daDkMHKem){
		
			    					//set new ket qua
			    					ketqua.getKetqua().setKetQua("K");
			    					ketqua.getKetqua().setKetQuaLyDo(lyDo);
			    					ketqua.getKetqua().setXacNhanYN(false);
			    					//da co loi xay ra, khong can kiem tra tiep
			    					error = true;
			    				}
		    				}
		    				

		    				/********************************************************
		    				 * kiem tra si so lop
		    				 ********************************************************/
		    				if(!error){
			    				cons.clear();
			    				columns.clear();
			    				
			    				{ 
			    			        ConditionReport con = new ConditionReport(); 
			    			        con.setCol("id");
			    			        con.setValue1(ketqua.getKetqua().getNLMHId().getTKBId().getId());
			    			        con.setOperator("eq");
			    			        cons.add(con);	
			    			    }
			    				columns.add("soHVDangKy");
			    				
			    				List<Object> soHVDangKys = tblThongTinTKBService.findColumntblThongTinTKBByCondition(cons, columns, 1, 1);
			    				if(soHVDangKys != null && soHVDangKys.size() > 0){
				    				//neu qua si so cho hpep
			    					int soHVDangKy = (Integer) soHVDangKys.get(0);
			    					
			    					//neu vuot si so Max
			    					if((ketqua.getKetqua().getNLMHId().getTKBId().getPhanTramDoi() + ketqua.getKetqua().getNLMHId().getTKBId().getSiSoMax()) < soHVDangKy)
			    					{
			    						ketqua.getKetqua().setKetQua("V");
				    					ketqua.getKetqua().setKetQuaLyDo("Tại thời điểm xét nhóm đã quá sĩ số cho phép không ĐK được");
				    					
				    					//da co loi xay ra, khong kiem tra tiep
				    					error = true;
			    					}else if((ketqua.getKetqua().getNLMHId().getTKBId().getPhanTramXepLop() + ketqua.getKetqua().getNLMHId().getTKBId().getSiSoMax()) < soHVDangKy)
			    					{
			    						ketqua.getKetqua().setKetQua("?");
				    					ketqua.getKetqua().setKetQuaLyDo("Quá sĩ số cho phép, đợi tách lớp!");

					    				ketqua.getKetqua().setXacNhanYN(true);
					    				try {
						    				if(!ketqua.getKetqua().getNLMHId().equals(new Long(ketqua.getKetqua().getMaDieuChinh2())))
								    			soHVDangKy++;
										} catch (Exception e) {
											System.out.println("loi nlmhid");
										}
						    			
				    					
			    					}else if(ketqua.getKetqua().getNLMHId().getTKBId().getSiSoMax()>0 
				    						&& ketqua.getKetqua().getNLMHId().getTKBId().getSiSoMax() <= soHVDangKy){

			    						ketqua.getKetqua().setKetQua("+");
				    					ketqua.getKetqua().setKetQuaLyDo("Quá sĩ số cho phép, đợi mở rộng lớp!");

					    				ketqua.getKetqua().setXacNhanYN(true);
					    				try {
						    				if(!ketqua.getKetqua().getNLMHId().equals(new Long(ketqua.getKetqua().getMaDieuChinh2())))
								    			soHVDangKy++;
										} catch (Exception e) {
											System.out.println("loi nlmhid");
										}
						    			
				    					
				    				}else{
					    				ketqua.getKetqua().setXacNhanYN(true);
						    			try {
						    				if(!ketqua.getKetqua().getNLMHId().equals(new Long(ketqua.getKetqua().getMaDieuChinh2())))
								    			soHVDangKy++;
										} catch (Exception e) {
											System.out.println("loi nlmhid");
										}
						    			
				    				}
				    				ketqua.getKetqua().getNLMHId().getTKBId().setSoHVDangKy(soHVDangKy);
			    				}
		    				}
		    				
		    				//cap nhat thong tin xac nhan
		    				if(!error && ketqua.getKetqua().isXacNhanYN()){
			    				ketqua.getKetqua().setMaDieuChinh2("");
			    				ketqua.getKetqua().setTinhTrang("RC");
		    				}

		    				//luu DB
		    				try {

				    			tblDKMHKetquaService.updatetblDKMHKetqua(ketqua.getKetqua());
							} catch (Exception e) {
								e.printStackTrace();
								System.out.println(ketqua.getKetqua().getId());
								System.out.println(ketqua.getKetqua().getNLMHId().getTen());
								
							}
		    			}
		    			
//		    			//ghi log dang ky
//		    			if(ketqua.getKetqua().isXacNhanYN()){
//			   				TblDKMHLogKetQuaDangKy tblDKMHLogKetQuaDangKy = new TblDKMHLogKetQuaDangKy();
//		    				tblDKMHLogKetQuaDangKy.setDotDKId(ketqua.getKetqua().getDotDKId());
//		    				tblDKMHLogKetQuaDangKy.setHocVienId(ketqua.getKetqua().getHocVienId());
//		    				tblDKMHLogKetQuaDangKy.setNlmhId(ketqua.getKetqua().getNLMHId());
//		    				tblDKMHLogKetQuaDangKy.setMaDieuChinh(ketqua.getKetqua().getMaDieuChinh());
//		    				
//		    				DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
//		    				Date dd = new Date();
//		    				GregorianCalendar c = new GregorianCalendar();
//		    				c.setTime(dd);
//		    				XMLGregorianCalendar d = DatatypeFactory.newInstance()
//		    						.newXMLGregorianCalendar(c);
//		    				tblDKMHLogKetQuaDangKy.setNgayGhiLog(d);
//		    				
//		    				tblDKMHLogKetQuaDangKies.add(tblDKMHLogKetQuaDangKy);
//		    			}
		    		}
	
		    }
		    
		    if(tblDKMHLogKetQuaDangKies != null && tblDKMHLogKetQuaDangKies.size() > 0)
		    	tblDKMHLogKetQuaDangKyService.createListtblDKMHLogKetQuaDangKy(tblDKMHLogKetQuaDangKies);
		    
		    List<KetQuaDangKy> temp = new ArrayList<KetQuaDangKy>(ketQuaDangKys);
		    for(KetQuaDangKy t : temp){
		    	if(t.getKetqua().getMaDieuChinh().equals("BH") || t.getKetqua().getMaDieuChinh().equals("HH")){
		    		ketQuaDangKys.remove(t);
	
				}
		    		
		    }
		    
		    session.put("ketQuaDangKys", ketQuaDangKys);
		    TblCTDTDMTietHocService tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient");	    

	    	 List<ConditionReport> cons = new ArrayList<ConditionReport>();
			    cons.clear();
		    	{ 
			        ConditionReport con = new ConditionReport(); 
			        con.setCol("ma");
			        con.setValue1("0");
			        con.setOperator("ne");
			        cons.add(con);	
			   }
			    soTietTrongNgay = tblCTDTDMTietHocService.counttblCTDTDMTietHocByCondition(cons);

		    /*------------------------
		     * Tao File TXT de send mail
		     * Bo comment de chay chuc nang nay
		     */
//		    if(capnhat && ketQuaDangKys != null && ketQuaDangKys.size() > 0){
//		    	TblThongTinHocVien hocVien = ketQuaDangKys.get(0).getKetqua().getHocVienId();
//		    	TblDKMHDotDK dotDK = ketQuaDangKys.get(0).getKetqua().getDotDKId();
//		    
//		    	
//		    	DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); 
//			    Date today = new Date();
//			    String directoryName = df.format(today);
//			    File theDir = new File(directoryName);
//
//			    //tao directory neu chua tao
//			    if (!theDir.exists()) {
//			      boolean result = theDir.mkdir();  
//			    }
//			    
//			    GregorianCalendar c = new GregorianCalendar(); 
//			    c.setTime(today);
//			    XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
//			    
//		    	DateFormat dtf = new SimpleDateFormat("ddMMyyyyhhmmss"); 
//			    String filename = hocVien.getMSSV()+ "_" + dtf.format(today) + ".txt" ;
//			    
//			    //ghi file txt
//			    BufferedWriter  out = new BufferedWriter(new OutputStreamWriter(
//			    	    new FileOutputStream(directoryName+"/"+ filename), "UTF-8"));
//
//			    out.write("S\u1EED d\u1EE5ng font Courier New \u0111\u1EC3 xem file n\u00E0y (N\u1EBFu kh\u00F4ng xem \u0111\u01B0\u1EE3c) ");
//			    out.newLine();
//			    out.newLine();
//	
//			    out.write("Ph\u00F2ng \u0111\u00E0o t\u1EA1o - Tr\u01B0\u1EDDng \u0110\u1EA1i H\u1ECDc B\u00E1ch Khoa Tp.HCM ");
//			    out.newLine();
//			    out.newLine();
//			    
//			    out.write("D\u1ECBch v\u1EE5 \u0111\u0103ng k\u00FD m\u00F4n h\u1ECDc ");
//			    out.newLine();
//			    out.newLine();
//			    
//			    out.write("\u0110\u1EE3t \u0111\u0103ng k\u00FD: " +dotDK.getTen()+ "");
//			    out.newLine();
//			    out.write("Hi\u1EC7u ch\u1EC9nh v\u00E0o l\u00FAc: "+d.getHour()+" gi\u1EDD "+d.getMinute()+" ph\u00FAt, ng\u00E0y "+df.format(today)+ "  ");
//			    out.newLine();
//			    out.write("Sinh vi\u00EAn: " +hocVien.getMSSV()+ " - " +hocVien.getHoTen()+ " ");
//			    out.newLine();
//			    out.write("B\u1EA3ng ghi nh\u1EADn k\u1EBFt qu\u1EA3 hi\u1EC7u ch\u1EC9nh - b\u1ED5 sung: ");
//			    out.newLine();
//			    out.newLine();
//			    
//			    out.write("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//			    out.newLine();
//		    	out.write("M\u00E3 m\u00F4n h\u1ECDc	 Nh\u00F3m h\u1ECDc	T\u00EAn m\u00F4n h\u1ECDc			K\u1EBFt qu\u1EA3\u0009M\u00F4n b\u00E0i t\u1EADp\u0009Nh\u00F3m b\u00E0i t\u1EADp	Nh\u00F3m \u0111\u1ED5i		T\u00ECnh tr\u1EA1ng\u0009X\u1EED l\u00FD		Ghi ch\u00FA ");
//			    out.newLine();
//		    	
//			    for(KetQuaDangKy kq : ketQuaDangKys){
//
//				    out.write("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//				    out.newLine();
//		    		out.write(""+kq.getKetqua().getNLMHId().getMonHocId().getMa()+"		--		"+kq.getKetqua().getNLMHId().getMonHocId().getTen()+"		"+kq.getKetqua().getKetQua()+"	--		--		--		"+kq.getKetqua().getMaDieuChinh()+"		"+kq.getKetqua().getTinhTrang()+"		-- ");
//				    out.newLine();
//			    }
//	
//
//			    out.write("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//		    	out.newLine();
//		    	out.write("B\u1EA3ng ghi ch\u00FA c\u00E1c k\u00FD hi\u1EC7u c\u1ED9t k\u1EBFt qu\u1EA3, sinh vi\u00EAn tham kh\u1EA3o t\u1EA1i trang http://www.aao.hcmut.edu.vn/aao_tkb.php ");
//		    	out.newLine();
//	
//			    out.newLine();
//			    
//			    out.close();
//		    }//end-if capnhat == true
		    
		    /*
		     * Send mail tu dong
		     */
//		    if(capnhat && ketQuaDangKys != null && ketQuaDangKys.size() > 0){
//		    	TblThongTinHocVien hocVien = ketQuaDangKys.get(0).getKetqua().getHocVienId();
//		    	TblDKMHDotDK dotDK = ketQuaDangKys.get(0).getKetqua().getDotDKId();
//		    
//		    	DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); 
//			    Date today = new Date();
//			    
//			    GregorianCalendar c = new GregorianCalendar(); 
//			    c.setTime(today);
//			    XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
//			    
//			    String subject = "DKMHTT-" +dotDK.getMa() + "-" + df.format(today);
//			    
//		    	String content = "";
//
//			    content += "Phòng đào tạo - Trường đại học Bách Khoa Tp.HCM ";
//			    content += "<br/>" ;
//			    content += "<br/>" ;
//			    
//			    content += "Dịch vụ đăng ký môn học trực tuyến!";
//			    content += "<br/>" ;
//			    content += "<br/>" ;
//			    
//			    content += "Đợt đăng ký: " +dotDK.getTen()+ "";
//			    content += "<br/>" ;
//			    content += "Hiệu chỉnh vào lúc: "+d.getHour()+" giờ "+d.getMinute()+" phút, ngày "+df.format(today)+ "  ";
//			    content += "<br/>" ;
//			    content += "Sinh viên: " +hocVien.getMSSV()+ " - " +hocVien.getHoTen()+ " ";
//			    content += "<br/>" ;
//			    content += "Bảng ghi nhận kết quả hiệu chỉnh - bổ sung: ";
//			    content += "<br/>" ;
//			    content += "<br/>" ;
//			    
//			    content += "------------------------------------------------------------------------------------------------------------------------------------------------------";
//			    content += "<br/>" ;
//
//			    content += "<table width='100%'>";
//			    content += "<tr>";
//		    	content +="<th align='left'>Mã môn học </th>" 
//					    + "<th align='left'>Tên môn học  </th>" 
//				    	+ "<th align='left'>Nhóm học  </th>" 
//					    + "<th align='left'>Kếtquả  </th>" 
//				    	+ "<th align='left'>Môn bài tập  </th>" 
//					    + "<th align='left'>Nhóm bài tập </th>" 
//				    	+ "<th align='left'>Nhóm đổi </th>" 
//				    	+ "<th align='left'>Ghi chú </th> ";
//		    	
//			    content += "</tr>" ;
//		    	
//			    for(KetQuaDangKy kq : ketQuaDangKys){
//
//				    content += "<tr>" ;
//				    
//		    		content +="<td>"+kq.getKetqua().getNLMHId().getMonHocId().getMa()+" </td>" ;
//		    		content += "<td> "+kq.getKetqua().getNLMHId().getMonHocId().getTen()+" </td>" ;
//		    		content += "<td> "+kq.getKetqua().getNLMHId().getMa() +" </td>" ;
//		    		content += "<td> "+kq.getKetqua().getKetQua()+" </td>" ;
//		    		content += "<td> -- </td>" ;
//		    		content	+= "<td> -- </td>" ;
//		    		content += "<td> -- </td>" ;
//		    		content += "<td> " +kq.getKetqua().getKetQuaLyDo()==null? "--":kq.getKetqua().getKetQuaLyDo() + " </td>";;
//		    		
//				    content += "</tr>" ;
//			    }
//			    content += "</table>" ;
//
//			    content += "-----------------------------------------------------------------------------------------------------------------------------------------------------";
//		    	content += "<br/>" ;
//		    	content += "<br/>" ;
//	
//			    content += "<br/>" ;
//			    
////			    //neu email ton tai se gui mail
////			    if(hocVien.getEmail() != null && !hocVien.getEmail().equals("")){
////			    	SendMail.send(hocVien.getEmail() + ",vietnguyen3107@gmail.com", subject, content);
////			    }else{
////			    	//send mail tam cho email vietnguyen3107@gmail.com
////			    	SendMail.send("vietnguyen3107@gmail.com,xuanlochoang2000@gmail.com", subject, content);
////			    	
//////			    	//thong bao loi
//////			    	code = "ERROR";
//////					// qua so tin chi cho phep
//////					msg = "<b>Không thể gửi mail </b> <br/>";
//////					msg += "Bạn chưa có thông tin email, vui lòng cung cấp cho ban quản lý!!!";
//////					return Action.ERROR;
////			    }
//			    
//		    }//end-if capnhat == true

	    }
	    //ngoai han dang ky, bao loi cho nguoi dung
	    else{
	    	code = "ERROR";
			msg = "<b>Không thể xác nhận </b> <br/> Đã hết hạn đăng ký, kết quả đăng ký dự kiến trong thời gian này sẽ bị hủy sau khi bạn đăng xuất!";
			return Action.ERROR;
	    }

		return Action.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			code = "ERROR";
			msg = e.getMessage();
			return Action.ERROR;
		}


	}


	
	
	
	
	
}
